package com.example.mall.web.controller;

import com.alibaba.fastjson.JSON;
import com.example.mall.domain.Order;
import com.example.mall.dto.KillPlaceOrderDto;
import com.example.mall.dto.OrderDto;
import com.example.mall.dto.PlaceOrderDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.OrderQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.result.JSONResult;
import com.example.mall.service.IOrderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    public IOrderService orderService;

    /**
     * 普通下单
     */
    @RequestMapping(value="/placeOrder",method= RequestMethod.POST)
    @ApiOperation(value = "普通下单",notes = "普通下单",httpMethod = "POST")
    public JSONResult placeOrder(@RequestBody PlaceOrderDto dto){
        return orderService.placeOrder(dto);
    }


    /**
     * 秒杀下单
     */
    @RequestMapping(value="/killPlaceOrder",method= RequestMethod.POST)
    @ApiOperation(value = "秒杀下单",notes = "秒杀下单",httpMethod = "POST")
    public JSONResult killPlaceOrder(@RequestBody KillPlaceOrderDto dto){
        return orderService.killPlaceOrder(dto);
    }

    /**
     * 根据单号查询订单
     */
    @RequestMapping(value="/one/{orderNo}",method= RequestMethod.GET)
    @ApiOperation(value = "根据单号查询订单",notes = "根据单号查询订单",httpMethod = "GET")
    public JSONResult selectByOrderNo(@PathVariable("orderNo")String orderNo){
        return JSONResult.success(JSON.toJSONString(orderService.selectByOrderNo(orderNo)));
    }

    /**
     * 分页查询订单主表信息
     * @param orderQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询订单主表信息",notes = "分页查询订单主表信息",httpMethod = "GET")
    public JSONResult queryPage(OrderQuery orderQuery){
        PageDTO<OrderDto> orderDtoPageDTO = orderService.queryPage(orderQuery);
        return JSONResult.success("查询成功",orderDtoPageDTO);
    }

    /**
     * 根据id查询订单主表信息
     * @param orderId
     * @return
     */
    @GetMapping("query/{orderId}")
    @ApiOperation(value = "根据id查询订单主表信息",notes = "根据id查询订单主表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("orderId")Long orderId){
        //1.根据id查询用户信息
        if (orderId == null){
            throw new GlobalCustomerException(ErrorEnumCode.ORDERID_NOT_ID);
        }
        Order order = orderService.getById(orderId);
        return JSONResult.success("查询成功",order);
    }

    /**
     * 根据id查询订单主表信息
     * @param orderId
     * @return
     */
    @GetMapping("queryOrderById/{orderId}")
    @ApiOperation(value = "根据id查询订单主表信息",notes = "根据id查询订单主表信息",httpMethod = "GET")
    public JSONResult queryOrderById(@PathVariable("orderId")Long orderId){
        //1.根据id查询用户信息
        if (orderId == null){
            throw new GlobalCustomerException(ErrorEnumCode.ORDERID_NOT_ID);
        }
        Order order = orderService.queryOrderById(orderId);
        return JSONResult.success("查询成功",order);
    }

    /**
     * 查询全部订单主表信息
     * @param
     * @return
     */
    @GetMapping("queryOrderList")
    @ApiOperation(value = "查询全部订单主表信息",notes = "查询全部订单主表信息",httpMethod = "GET")
    public JSONResult queryOrderList(){
        List<Order> orders = orderService.queryOrderList();
        return JSONResult.success("查询成功",orders);
    }

    /**
     * 新增订单主表
     * @param orderDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增订单主表",notes = "新增订单主表",httpMethod = "POST")
    public JSONResult insert(@RequestBody OrderDto orderDto){
        orderService.insert(orderDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改订单主表信息
     * @param orderDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改订单主表信息",notes = "修改订单主表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody OrderDto orderDto){
        orderService.update(orderDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除订单主表信息
     * @param orderId
     * @return
     */
    @PostMapping("delete/{orderId}")
    @ApiOperation(value = "删除订单主表信息",notes = "删除订单主表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("orderId") Long orderId) {
        orderService.delete(orderId);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除订单主表信息
     * @param orderIds
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除订单主表信息",notes = "批量删除订单主表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> orderIds) {
        orderService.deleteBatch(orderIds);
        return JSONResult.success("批量删除成功");
    }
}
